package com.wh.order.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.basic.utils.LoginContext;
import com.wh.order.dto.OrderListQueryDto;
import com.wh.order.pojo.OrderList;
import com.wh.org.dao.IEmployeeDao;
import com.wh.org.dao.ShopDao;
import com.wh.org.pojo.Department;
import com.wh.org.pojo.Employee;
import com.wh.org.pojo.Shop;
import com.wh.pet.mapper.PetMapper;
import com.wh.pet.mapper.SearchMasterMsgMapper;
import com.wh.pet.pojo.Pet;
import com.wh.pet.pojo.SearchMasterMsg;
import com.wh.user.pojo.LoginInfo;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.wh.order.pojo.OrderPetAcquisition;
import com.wh.order.service.IOrderPetAcquisitionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wh.order.mapper.OrderPetAcquisitionMapper;
import com.wh.order.dto.OrderPetAcquisitionDto;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;


    @Override
    public List<OrderPetAcquisition>findAll(){
        return orderPetAcquisitionMapper.selectList(null);
    }

    @Override
    public IPage<OrderPetAcquisition>findByPage(OrderPetAcquisitionDto orderPetAcquisitionDto, HttpServletRequest request){
        //1.创建查询条件
        QueryWrapper<OrderPetAcquisition> qw = new QueryWrapper<>();

        //2.获取登录用户
//        LoginInfo logininfo = LoginContext.getLogininfo(request);
//        Employee employee = employeeDao.selectOne(new QueryWrapper<Employee>().eq("logininfo_id", logininfo.getId()));

        //3.判断登录员工是那种角色（平台人员/店铺人员）
        // employee中shop_id不为null，则表示是店铺人员，只能查询当前店铺信息
//        Long shopId = employee.getShopId();
//        if (shopId != null) {
//            //添加店铺Id作为查询条件
//            qw.eq("shop_id", shopId);
//
//            //4.判断店铺人员身份
//            //查询店铺信息
//            Shop shop = shopDao.selectById(shopId);
//            //判断店铺管理员和当前用户是否是同一个用户，如果不是，就说明当前登录的是普通店员
//            if (!shop.getAdminId().equals(employee.getId())) {
//                //普通店员只能查询自己的信息
//                qw.eq("emp_id", employee.getId());
//            }
//        }

        //5.封装关键字条件
//        if (ObjectUtil.isNotEmpty(orderPetAcquisitionDto.getKeyword())) {
//            qw.and(kw -> kw.like("digest", orderPetAcquisitionDto.getKeyword())
//                    .or()
//                    .like("address", orderPetAcquisitionDto.getKeyword()));
//        }

        //1.创建查询条件
        qw.like("order_sn", orderPetAcquisitionDto.getKeyword());
        qw.or();
        qw.like("customer_name", orderPetAcquisitionDto.getKeyword());
        //2.创建分页参数

        //6.组件分页数据
        IPage<OrderPetAcquisition> page = new Page<>(orderPetAcquisitionDto.getCurrentPage(), orderPetAcquisitionDto.getPageSize());
//        page.setRecords(orderPetAcquisitionMapper.findByPage(page, qw));
//        page.setTotal(orderPetAcquisitionMapper.selectCount(qw));
        //7.返回
        System.err.println(page);
        return page;
    }

    @Transactional
    @Override
    public void cancel(Long id) {
        //1.查询源数据
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.selectById(id);
        //2.修改状态(取消订单)
        orderPetAcquisition.setState(-1);
        //3.修改
        orderPetAcquisitionMapper.updateById(orderPetAcquisition);

        //修改寻主消息状态为3,进入寻主池
        UpdateWrapper<SearchMasterMsg> uw = new UpdateWrapper<>();
        uw.set("state", 3);
        uw.set("shop_id",null);
        uw.eq("id", orderPetAcquisition.getSearchMasterMsgId());
        searchMasterMsgMapper.update(null, uw);
    }

    @Override
    public void confirm(OrderPetAcquisition orderPetAcquisition) {
        //1.查询源数据
        OrderPetAcquisition opa = orderPetAcquisitionMapper.selectById(orderPetAcquisition.getId());
        //2.修改状态(已完成)
        opa.setState(1);
        //3.修改
        orderPetAcquisitionMapper.updateById(opa);

        //4.封装店铺宠物信息
        SearchMasterMsg msg = searchMasterMsgMapper.selectById(opa.getSearchMasterMsgId());

        //5.判断宠物是否存在
        Pet pet = petMapper.selectOne(new QueryWrapper<Pet>().eq("search_master_msg_id", msg.getId()));
        if (pet != null) {
            pet.setName(msg.getName());        //宠物名称
            pet.setCostprice(orderPetAcquisition.getPrice());    //宠物价格
            pet.setResources(msg.getResources());    //宠物图片
            pet.setShopId(msg.getShopId());            //所属店铺，如果被收养，则店铺Id为null
            pet.setUserId(msg.getUserId());            //原主人，被收养后为新主人Id
            pet.setSearchMasterMsgId(msg.getId());    //寻主消息Id
            //宠物已经存在
            petMapper.updateById(pet);
        } else {
            pet = new Pet();
            pet.setName(msg.getName());        //宠物名称
            pet.setCostprice(orderPetAcquisition.getPrice());    //宠物价格
            pet.setResources(msg.getResources());    //宠物图片
            pet.setShopId(msg.getShopId());            //所属店铺，如果被收养，则店铺Id为null
            pet.setUserId(msg.getUserId());            //原主人，被收养后为新主人Id
            pet.setSearchMasterMsgId(msg.getId());    //寻主消息Id
            petMapper.insert(pet);
        }


        //5.修改寻主消息状态为2,变为已完成
        msg.setState(2);
        searchMasterMsgMapper.updateById(msg);
    }
}
